package cn.droidlover.xdroidmvp.util;

import java.io.Serializable;
import java.util.Locale;

import cn.droidlover.xdroidmvp.BaseApplication;
import cn.droidlover.xdroidmvp.cache.SharedPref;

/**
 * @author by DELL
 * @date on 2017/11/29
 * @describe SharedPreferences 数据保存管理类
 */

public class BaseSPManager {
    public static final String FOLLOW_SYSTEM_LANGUAGE = "follow_system_language";//语言
    public static final String LANGUAGE = "language";//语言
    public static final String LANGUAGE_COUNTRY = "language_country";//语言_国家地区
    public static final String DAY_NIGHT_MODE = "day_night_mode";//日夜模式
    public static final String IS_FIRST_LAUNCH = "is_first_launch";//是否第一次启动
    public static final String VERSION_CODE = "version_code";//版本号


    /*** 是否跟随系统 */
    public static boolean isFollowSystemLanguage() {
        return SharedPref.getInstance(BaseApplication.getInstance()).getBoolean(FOLLOW_SYSTEM_LANGUAGE,true);
    }

    public static void setFollowSystemLanguage(boolean isFollow) {
        SharedPref.getInstance(BaseApplication.getInstance()).putBoolean(FOLLOW_SYSTEM_LANGUAGE, isFollow);
    }

    /***  语言 默认系统语言*/
    public static Locale getLanguage() {
        boolean isSystem = isFollowSystemLanguage();
        if (isSystem) return Locale.getDefault();
        String language = SharedPref.getInstance(BaseApplication.getInstance()).getString(LANGUAGE, Locale.getDefault().getLanguage());//默认简体中文
        String country = SharedPref.getInstance(BaseApplication.getInstance()).getString(LANGUAGE_COUNTRY, Locale.getDefault().getCountry());//默认简体中文
        return new Locale(language, country);
    }


    public static void setLanguage(Locale language) {
        SharedPref.getInstance(BaseApplication.getInstance()).putString(LANGUAGE, language.getLanguage());
        SharedPref.getInstance(BaseApplication.getInstance()).putString(LANGUAGE_COUNTRY, language.getCountry());
    }


    /*** 日夜模式 */
    public static boolean isNightMode() {
        return  SharedPref.getInstance(BaseApplication.getInstance()).getBoolean(DAY_NIGHT_MODE, false);
    }

    public static void setNightMode(boolean nightMode) {
        SharedPref.getInstance(BaseApplication.getInstance()).putBoolean(DAY_NIGHT_MODE, nightMode);
    }

    /*** 是否第一次启动 */
    public static boolean isFirstLaunch() {
        return SharedPref.getInstance(BaseApplication.getInstance()).getBoolean(IS_FIRST_LAUNCH, true);
    }

    public static void setIsFirstLaunch(boolean isFirstLunch) {
        SharedPref.getInstance(BaseApplication.getInstance()).put(IS_FIRST_LAUNCH, isFirstLunch);
    }

    /*** 是否新版本 */
    public static boolean isNewVersion() {
        int versionCode =SharedPref.getInstance(BaseApplication.getInstance()).getInt(VERSION_CODE, 0);
        int curCode = AppUtil.getPackageInfo(BaseApplication.getInstance()).versionCode;
        return curCode > versionCode;
    }

    public static void updateVersionCode() {
        int curCode = AppUtil.getPackageInfo(BaseApplication.getInstance()).versionCode;
        SharedPref.getInstance(BaseApplication.getInstance()).putInt(VERSION_CODE, curCode);
    }

    /**
     * 保存泛型到本地,保存前移除现有数据
     * @param key
     * @param object
     */
    public static void putCustomData(String key, Object object){
        SharedPref.getInstance(BaseApplication.getInstance()).remove(key);
      //  SharedPref.getInstance(BaseApplication.getInstance()).putSerializableObject(key,object);
    }

    /**
     * 取出保存的数据，反序列化
     * @param key
     * @return
     */
//    public static Serializable getCustomData(String key){
//       // return SharedPref.getInstance(BaseApplication.getInstance()).getSerializableObject(key);
//    }

}
